package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.R.R;
import net.wanho.po.ProductType;
import net.wanho.service.ProductTypeService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor
@RestController
@RequestMapping("/productCategory")
public class ProductCategoryController {

    private final ProductTypeService productTypeService;

    // 获取商品分类树
    @GetMapping("product-category-tree")
    public R getProductCategoryTree() {
        List<ProductType> productList = productTypeService.getProductCategoryTree();
        return R.ok(productList);
    }

    // 判断分类编码是否已存在
    @GetMapping("verify-type-code")
    public R verifyTypeCode(String typeCode) {
        boolean flag = productTypeService.lambdaQuery().eq(ProductType::getTypeCode, typeCode).exists();
        return R.ok(flag);
    }

    // 添加分类
    @PostMapping("type-add")
    public R addType(@RequestBody ProductType productType) {
        boolean flag = productTypeService.save(productType);
        return R.ok(flag);
    }

    // 根据ID查询分类信息
    @GetMapping("{id}")
    public R getTypeById(@PathVariable("id") Integer id) {
        ProductType productType = productTypeService.getById(id);
        return R.ok(productType);
    }

    // 编辑分类信息
    @PutMapping("type-update")
    public R updateType(@RequestBody ProductType productType) {
        boolean flag = productTypeService.updateById(productType);
        return R.ok(flag);
    }

    // 删除分类
    @DeleteMapping("type-delete/{id}")
    public R deleteType(@PathVariable("id") Integer id) {
        boolean flag = productTypeService.removeById(id);
        return R.ok(flag);
    }
}
